package com.cheer.controller;

import com.cheer.dto.AccountBackupUpdateDTO;
import com.cheer.dto.AccountUpdateDTO;
import com.cheer.dto.PasswordUpdateDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.AccountService;
import com.cheer.service.AdminService;
import com.cheer.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    /**
     * 修改手机号
     */
    @PostMapping("/phone")
    public Result updatePhone(@RequestBody @Valid AccountUpdateDTO accountUpdateDTO) {
        accountService.updatePhone(userInfoHolder.getAccountId(), accountUpdateDTO);
        return Result.success();
    }

    /**
     * 修改备用账号
     */
    @PostMapping("/backup")
    public Result updateBackupAccount(@RequestBody @Valid AccountBackupUpdateDTO accountBackupUpdateDTO) {
        accountService.updateBackup(userInfoHolder.getAccountId(), accountBackupUpdateDTO);
        return Result.success();
    }

    /**
     * 获取验证问题
     */
    @GetMapping("/question")
    public Result<String> getQuestion() {
        String securityQuestion = accountService.getSecurityQuestion(userInfoHolder.getAccountId());
        return Result.success(securityQuestion);
    }

    /**
     * 修改密码
     */
    @PostMapping("/password")
    public Result updatePassword(@RequestBody @Valid PasswordUpdateDTO passwordUpdateDTO) {
        accountService.updatePassword(userInfoHolder.getAccountId(), passwordUpdateDTO);
        return Result.success();
    }

    /**
     * 批量导入管理员用户
     */
    @PostMapping("/import/accounts")
    public Result importAccounts(MultipartFile file) {
        adminService.importAdmin(userInfoHolder.getAdminOrgId(), file);
        return Result.success();
    }
}
